package com.unit.handler;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.unit.util.TimeUtil;

public class CalendarHandler implements DataObjectHandler {

	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Override
	public Object convert(String data, Class<?> propType) {
		if (Calendar.class.isAssignableFrom(propType)) {
			try {
				Calendar result = Calendar.getInstance();
				synchronized (dateFormat) {
					if (TimeUtil.isDateString(data)) {
						result.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(data));
					}else {
						result.setTime(dateFormat.parse(data));
					}
					
				}
				return result;
			} catch (Exception e) {
				throw new RuntimeException("Calendar时间格式转换异常："+e.getMessage());		
			}
		}
		return null;
	}

}
